home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / interp / handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  4.9 KB  |  183 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: handler.c,v 1.6 94/06/27 16:31:56 wlott Exp $
  27. *
  28. * This file implements the low level support for exception handlers.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include "mindy.h"
  33. #include "class.h"
  34. #include "gc.h"
  35. #include "obj.h"
  36. #include "def.h"
  37. #include "thread.h"
  38. #include "func.h"
  39. #include "list.h"
  40. #include "bool.h"
  41. #include "sym.h"
  42. #include "type.h"
  43. #include "handler.h"
  44.  
  45.  
  46. static obj_t obj_HandlerClass = NULL;
  47.  
  48. static void push_handler(obj_t method, struct thread *thread, obj_t *args)
  49. {
  50.     obj_t *old_sp = args-1;
  51.     obj_t type = args[0];
  52.     obj_t func = args[1];
  53.     obj_t test = args[2];
  54.     obj_t init_args = args[3];
  55.     obj_t handler = alloc(obj_HandlerClass, sizeof(struct handler));
  56.  
  57.     HANDLER(handler)->type = type;
  58.     HANDLER(handler)->function = func;
  59.     HANDLER(handler)->test = test;
  60.     HANDLER(handler)->init_args = init_args;
  61.     HANDLER(handler)->next = thread->handlers;
  62.     thread->handlers = handler;
  63.  
  64.     thread->sp = old_sp;
  65.     do_return(thread, old_sp, old_sp);
  66. }
  67.  
  68. static void current_handler(struct thread *thread, int nargs)
  69. {
  70.     obj_t *old_sp = thread->sp - 1;
  71.  
  72.     assert(nargs == 0);
  73.     *old_sp = thread->handlers;
  74.  
  75.     do_return(thread, old_sp, old_sp);
  76. }
  77.  
  78. static obj_t handler_type(obj_t handler)
  79. {
  80.     return HANDLER(handler)->type;
  81. }
  82.  
  83. static obj_t handler_function(obj_t handler)
  84. {
  85.     return HANDLER(handler)->function;
  86. }
  87.  
  88. static obj_t handler_test(obj_t handler)
  89. {
  90.     return HANDLER(handler)->test;
  91. }
  92.  
  93. static obj_t handler_init_args(obj_t handler)
  94. {
  95.     return HANDLER(handler)->init_args;
  96. }
  97.  
  98. static obj_t handler_next(obj_t handler)
  99. {
  100.     return HANDLER(handler)->next;
  101. }
  102.  
  103. static void pop_handler(struct thread *thread, int nargs)
  104. {
  105.     obj_t *old_sp = thread->sp - 1;
  106.  
  107.     assert(nargs == 0);
  108.  
  109.     thread->handlers = HANDLER(thread->handlers)->next;
  110.  
  111.     thread->sp = old_sp;
  112.     do_return(thread, old_sp, old_sp);
  113. }
  114.  
  115.  
  116.  
  117. /* GC stuff. */
  118.  
  119. static int scav_handler(struct object *obj)
  120. {
  121.     struct handler *handler = (struct handler *)obj;
  122.  
  123.     scavenge(&handler->type);
  124.     scavenge(&handler->function);
  125.     scavenge(&handler->test);
  126.     scavenge(&handler->init_args);
  127.     scavenge(&handler->next);
  128.  
  129.     return sizeof(struct handler);
  130. }
  131.  
  132. static obj_t trans_handler(obj_t handler)
  133. {
  134.     return transport(handler, sizeof(struct handler));
  135. }
  136.  
  137. void scavenge_handler_roots(void)
  138. {
  139.     scavenge(&obj_HandlerClass);
  140. }
  141.  
  142.  
  143. /* Init stuff. */
  144.  
  145. void make_handler_classes(void)
  146. {
  147.     obj_HandlerClass = make_builtin_class(scav_handler, trans_handler);
  148. }
  149.  
  150. void init_handler_classes(void)
  151. {
  152.     init_builtin_class(obj_HandlerClass, "<handler>", obj_ObjectClass, NULL);
  153. }
  154.  
  155. void init_handler_functions(void)
  156. {
  157.     define_constant("push-handler",
  158.             make_raw_method("push-handler",
  159.                     list2(obj_TypeClass, obj_FunctionClass),
  160.                     FALSE,
  161.                     list2(pair(symbol("test"), obj_False),
  162.                       pair(symbol("init-arguments"),
  163.                            obj_Nil)),
  164.                     FALSE, obj_Nil, obj_False, push_handler));
  165.     define_constant("current-handler",
  166.             make_raw_function("current-handler", 0, FALSE, obj_False,
  167.                       FALSE, list1(obj_HandlerClass),
  168.                       obj_False, current_handler));
  169.     define_function("handler-type", list1(obj_HandlerClass), FALSE, obj_False,
  170.             FALSE, obj_ObjectClass, handler_type);
  171.     define_function("handler-function", list1(obj_HandlerClass), FALSE,
  172.             obj_False, FALSE, obj_ObjectClass, handler_function);
  173.     define_function("handler-test", list1(obj_HandlerClass), FALSE, obj_False,
  174.             FALSE, obj_ObjectClass, handler_test);
  175.     define_function("handler-init-args", list1(obj_HandlerClass), FALSE,
  176.             obj_False, FALSE, obj_ObjectClass, handler_init_args);
  177.     define_function("handler-next", list1(obj_HandlerClass), FALSE, obj_False,
  178.             FALSE, obj_ObjectClass, handler_next);
  179.     define_constant("pop-handler",
  180.             make_raw_function("pop-handler", 0, FALSE, obj_False,
  181.                       FALSE, obj_Nil, obj_False, pop_handler));
  182. }
  183.